package cn.cs.service.impl;

import cn.cs.client.AgentCapitalPoolDetailsClient;
import cn.cs.client.SysDtAreaClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.CapitalPoolDetails;
import cn.cs.service.CapitalPoolDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CapitalPoolDetailsServiceImpl implements CapitalPoolDetailsService {

    @Resource
    private AgentCapitalPoolDetailsClient capitalPoolDetailsClient;

    @Resource
    private SysDtAreaClient sysDtAreaClient;

    public CapitalPoolDetails getCapitalPoolDetailsById(String id) throws Exception {
        return capitalPoolDetailsClient.getCapitalPoolDetailsById(id);
    }

    public List<CapitalPoolDetails> getCapitalPoolDetailsListByMap(Map<String, Object> param) throws Exception {
        return capitalPoolDetailsClient.getCapitalPoolDetailsListByMap(param);
    }

    public Integer getCapitalPoolDetailsCountByMap(Map<String, Object> param) throws Exception {
        return capitalPoolDetailsClient.getCapitalPoolDetailsCountByMap(param);
    }

    public Integer addCapitalPoolDetails(CapitalPoolDetails capitalPoolDetails) throws Exception {
        capitalPoolDetails.setCreateDate(new Date());
        capitalPoolDetails.setModifyDate(new Date());
        return capitalPoolDetailsClient.addCapitalPoolDetails(capitalPoolDetails);
    }

    public Integer modifyCapitalPoolDetails(CapitalPoolDetails capitalPoolDetails) throws Exception {
        capitalPoolDetails.setModifyDate(new Date());
        return capitalPoolDetailsClient.modifyCapitalPoolDetails(capitalPoolDetails);
    }

    public Integer deleteCapitalPoolDetailsById(String id) throws Exception {
        return capitalPoolDetailsClient.deleteCapitalPoolDetailsById(id);
    }

    public Page<CapitalPoolDetails> queryCapitalPoolDetailsPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = capitalPoolDetailsClient.getCapitalPoolDetailsCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<CapitalPoolDetails> capitalPoolDetailsList = capitalPoolDetailsClient.getCapitalPoolDetailsListByMap(param);
        for(CapitalPoolDetails a:capitalPoolDetailsList){
            String areaId = a.getAreaId();
            Map<String, Object> map = sysDtAreaClient.getCityNameByAreaId(areaId);
            a.setProvinceName(map.get("provinceName").toString());
            a.setCityName(map.get("cityName").toString());
            a.setAreaName(map.get("areaName").toString());
        }
        page.setRows(capitalPoolDetailsList);
        return page;
    }

}
